// AUTHORS: Yiwei (Alan) Han, Kevin Lam 

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("IE5/6 not supported");
	}
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

var exam_req = getXmlHttpRequestObject();

function getExamContent(email) {
	var user = getCookie('user'); 
	if (user != null ){
		if (exam_req.readyState == 4 || exam_req.readyState == 0) {
			exam_req.open("GET", 'auth/ajax/exam?email='+email, true);
			exam_req.onreadystatechange = handleExamContent;
			exam_req.send(null);
		}
	}
}

function handleExamContent() {
	if (exam_req.readyState == 4) {
		var xmlDoc = exam_req.responseXML;
		var exam = document.getElementById('exam');
		if(xmlDoc.getElementsByTagName("response")[0].hasChildNodes()) {
			var parent = xmlDoc.getElementsByTagName("response")[0].getElementsByTagName("item");
			var html = "<tr class='exam-table-header'>";
			html += "<td>Course</td><td>Time</td><td>Building</td><td>Alpha Split</td>";
			html += "</tr>";
			exam.innerHTML += html;
				for (k = 0; k < parent.length; k++) {
					var dept = parent[k].getElementsByTagName("dept")[0].childNodes[0].nodeValue;
					var course = parent[k].getElementsByTagName("course")[0].childNodes[0].nodeValue;
					var section = parent[k].getElementsByTagName("section")[0].childNodes[0].nodeValue;
					var time = parent[k].getElementsByTagName("time")[0].childNodes[0].nodeValue;
					var building = parent[k].getElementsByTagName("building")[0].childNodes[0].nodeValue;
					var split = parent[k].getElementsByTagName("split")[0].childNodes[0].nodeValue;
					html = "<tr>";
					html += "<td>"+dept+" "+course+" "+section+"</td><td>"+time+"</td><td>"+unescape(building)+"</td><td>"+unescape(split)+"</td>";
					html += "</tr>";
					exam.innerHTML += html;
				}
			document.getElementById("exam-progress").className = "completed";
		}
	}
}
